package sms.dao;

import sms.entry.XueSheng;
import sms.ui.xuesheng.XueShengTianJia;
import java.sql.*;
public class XueShengDao {
    /*
    * 保存
    * */
    public void save(XueSheng xueSheng){
        try {
            Class.forName("com.mysql.jdbc.Driver");    //注册驱动
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "insert into xueshengxinxi(xuehao,name,sex,birthday) values(?,?,?,?) ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1, xueSheng.getXuehao());
            stmt.setObject(2,xueSheng.getName());
            stmt.setObject(3,xueSheng.getSex());
            stmt.setObject(4,xueSheng.getBirthday());
            stmt.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //删除
    public void remove(XueSheng xueSheng){
        try { Class.forName("com.mysql.jdbc.Driver");    //注册驱动
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "delete from xueshengxinxi where xuehao =? and name= ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,xueSheng.getXuehao());
            stmt.setObject(2,xueSheng.getName());
            stmt.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //修改
    public void update(XueSheng xueSheng) {
        try {
            Class.forName("com.mysql.jdbc.Driver");    //注册驱动
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "update xueshengxinxi set name =? where xuehao = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,xueSheng.getName());
            //stmt.setObject(2,xueSheng.getBirthday());
            stmt.setObject(2,xueSheng.getXuehao());
            stmt.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //查询
    public void chaxun(XueSheng xuesheng){
        try {
            Class.forName("com.mysql.jdbc.Driver");    //注册驱动
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "select id,xuehao,name,sex,birthday from xueshengxinxi where xuehao = ?";
            PreparedStatement stsm = conn.prepareStatement(sql);
            stsm.setObject(1,xuesheng.getXuehao());
            ResultSet rs  = stsm.executeQuery();;
            if (rs.next()){
                Object id = rs.getObject("id");
                Object xuehao = rs.getObject("xuehao");
                Object name = rs.getObject("name");
                Object sex = rs.getObject("sex");
                Object birthday = rs.getObject("birthday");
                System.out.println("id"+"   "+"学号"+"      "+"姓名"+"    "+"生日");
                System.out.println(id+"  "+"  "+xuehao+"  "+"   "+name+"  "+" "+birthday);
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
